package com.example.user.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.common.constants.UserConstants;
import com.example.common.util.R;
import com.example.common.util.ResultCodeRecord;
import com.example.feignapi.wemedia.WemediaClient;
import com.example.model.user.dto.AuthDto;
import com.example.model.user.pojo.User;
import com.example.model.user.pojo.UserRealname;
import com.example.user.mapper.UserMapper;
import com.example.user.mapper.UserRealnameMapper;
import com.example.user.service.UserRealnameService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class UserRealnameServiceImpl extends ServiceImpl< UserRealnameMapper, UserRealname > implements UserRealnameService {
    @Autowired
    private WemediaClient wemediaClient;
    @Autowired
    private UserMapper userMapper;

    @Override
    public boolean updateUserStatus(AuthDto authDto, Short userStatus) {
        // 1、检查参数
        if (authDto == null || authDto.getUserId() == null) {
            return R.error(ResultCodeRecord.PARAM_INVALID).isFlag();
        }
        // 2、修改认证状态
        UserRealname userRealname = new UserRealname();
        userRealname.setId(authDto.getUserId());
        userRealname.setUserStatus(userStatus);
        if (StringUtils.isNotBlank(authDto.getUserMessage())) {
            userRealname.setUserReason(authDto.getUserMessage());
        }
        updateById(userRealname);

        // 3、如果审核状态是9，就是成功，需要创建自媒体账户
        if (userStatus.equals(UserConstants.PASS_AUTH)) {
            R r = createUserAndAuth(authDto);
        }
        return false;
    }

    /**
     * 创建自媒体账户
     * @param authDto
     * @return
     */
    private R createUserAndAuth(AuthDto authDto) {
        Integer userRealnameId = authDto.getUserId();
        // 查询用户认证信息
        UserRealname userRealname = getById(userRealnameId);
        if (userRealname == null) {
            return R.error(ResultCodeRecord.DATA_NOT_EXIST);
        }
        // 查询app端用户信息
        Integer userId = userRealname.getUserId();
        User user = userMapper.selectById(userId);
        if (user == null) {
            return R.error(ResultCodeRecord.DATA_NOT_EXIST);
        }
        // 创建自媒体账户
        com.example.model.wemedia.pojo.User wemediaUser = wemediaClient.getUserByUserName(user.getUserName());
        if (wemediaUser == null) {
            wemediaUser = new com.example.model.wemedia.pojo.User();
            wemediaUser.setApUserId(user.getUserId());
            wemediaUser.setCreatedTime(new Date());
            wemediaUser.setPassword(user.getPassword());
            wemediaUser.setUserSalt(user.getUserPhone());
            wemediaUser.setUserStatus(9);
            wemediaClient.saveUser(wemediaUser);
        }
        user.setUserFlag((short) 1);
        userMapper.updateById(user);
        return R.ok(null);
    }
}
